﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZIC20Contract.DataType;
using ZICModel;
using System.Net;
using Infrastructure.Extension;
using Infrastructure.Log;
namespace ZICModel.ZISMessage
{
    public partial class ZISGateWay
    {
        ZISMessage.Message m_zisMessage;
        GPRSWebSvc.IDataservice m_gprsSvc;
        int destinationType = 2;
        string m_ip;

        public ZISGateWay()
        {
            Initialize();
        }
        public void Initialize()
        {
            m_gprsSvc = new GPRSWebSvc.IDataservice();
            IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress ipv4Address = ipEntry.AddressList.Where(i => i.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).FirstOrDefault();
            m_ip = ipv4Address.ToString();
            PokeZIS();
            CallLogger.LogMessage(typeof(ZISGateWay), "ZIS Gateway Initialized");
        }

        private void PokeZIS()
        {
            int temp = 0;
            m_gprsSvc.Login("gpsadmin", "gpsadmin", m_ip, ref temp);
        }
        public int SendGPRSMessage(string idc, ZISMsgType zisMsgType, GPRSMsgType gprsType, string msg)
        {
            m_zisMessage = new Message(idc,zisMsgType,gprsType,msg);
            return SendGPRSMessage(idc, zisMsgType);
            
        }

        public int SendPOIMessage(string idc, ZISMsgType zisMsgType, GPRSMsgType gprsType, POINavigation msg)
        {
            m_zisMessage = new Message(idc,zisMsgType,gprsType,msg);
            return SendGPRSMessage(idc, zisMsgType);
        }


        public int SendClearCodeMessage(string idc, ClearCodeType code)
        {
            return SendCommandMessage(idc, (int)code);
        }

        public int SendVehicleBrandCodeMessage(string idc, VehicleBrandCode code)
        {
            return SendCommandMessage(idc,(int)code);
        }


        public int SendSystemUpgradeMessage(string idc)
        {
            int orderID = new Random().Next(1, 100000);
            
            int result = m_gprsSvc.car_RemoteUpdate(orderID, 2, idc);
            CallLogger.LogMessage(typeof(ZISGateWay), string.Format("SendSystemUpgrade Succeed. idc = {0},result={1}", idc,result));
            return result;
        }

        private int SendCommandMessage(string idc, int command)
        {
            int orderID = new Random().Next(1, 10000);
            byte[] input = new byte[] { (byte)command, (byte)(command >> 8) };
            PokeZIS();
            int result = m_gprsSvc.car_sendIOCommand(orderID,
                                                    destinationType,
                                                    idc,
                                                    (int)ZISMsgType.CodeClear,
                                                    input);

            CallLogger.LogMessage(typeof(ZISGateWay), string.Format("SendCommandMsg Succeed. idc = {0},Command = Byte0:{1} Byte1:{2} result={3}",
                                                    idc,
                                                    (int)input[0],
                                                    (int)input[1],
                                                    result));
            return result;
        }

        private int SendGPRSMessage(string idc, ZISMsgType zisMsgType)
        {
            m_zisMessage.Process();
            int orderID = new Random().Next(1, 10000);
            Byte[] input = m_zisMessage.FormatedMessage.HexStringToByteArray();
            PokeZIS();
            int result = m_gprsSvc.car_sendIOCommand(orderID,
                                                     destinationType,
                                                     idc,
                                                     (int)zisMsgType,
                                                     input);
            CallLogger.LogMessage(typeof(ZISGateWay), string.Format("GPRSMsg {0} send to IDC={1}, ZISMsgType={2} Result={3} ZIS Gateway={4}",
                                                     orderID, 
                                                     idc,
                                                     (int)zisMsgType,
                                                     result,
                                                     m_gprsSvc.Url));
            return result;
            
        }

        

    }
}
